package com.hkts.naturalgasauthentication.management.controller;

import com.hkts.management.common.utils.CustomPage;
import com.hkts.management.common.utils.IDUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.naturalgasauthentication.management.entity.WebsiteInformation;
import com.hkts.naturalgasauthentication.management.service.WebsiteInformationService;
import com.hkts.naturalgasauthentication.utils.CustomPageUtil;
import com.hkts.naturalgasauthentication.utils.PageUtil;
import com.hkts.naturalgasauthentication.utils.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @ClassNameWebsiteInformationController
 * @Description
 * @Author yck
 * @Date2021-02-18 15:57
 * @Version V1.0
 **/
@Controller
@RequestMapping("/authentication/websiteInformation")
public class WebsiteInformationController {
    @Autowired
    private WebsiteInformationService websiteInformationServiceImpl;
    @ResponseBody
    @PostMapping("/saveWebsiteInformation")
    public Result saveWebsiteInformation(WebsiteInformation website, @RequestParam(name="fileField",required=true) MultipartFile fileField) throws IOException {
        website.setId(IDUtils.genImageName());
        website.setLogo(fileField.getBytes());
        websiteInformationServiceImpl.saveWebsiteInformation(website);
     return    ResultUtil.success("新增成功！");
    }

    @ResponseBody
    @PostMapping("/updateWebsiteInformation")
    public Result updateWebsiteInformation(WebsiteInformation website, @RequestParam(name="fileField",required=true) MultipartFile fileField) throws IOException {
        website.setLogo(fileField.getBytes());
        websiteInformationServiceImpl.updateWebsiteInformation(website);
        return    ResultUtil.success("修改成功！");
    }
    /*
    * 删除网站信息
    * */
    @ResponseBody
    @PostMapping("/deleteWebsiteInformation")
    public Result deleteWebsiteInformation(String id){
        websiteInformationServiceImpl.deleteWebsiteInformation(id);
        return    ResultUtil.success("删除成功！");
    }
    /*
    * 网站信息分页
    * */
    @ResponseBody
    @GetMapping("/getWebsiteInformationPage")
    public Result getWebsiteInformationPage(PageVo pageVo,WebsiteInformation websiteInformation){
        pageVo.setSort("websiteOrder");
        Pageable pageable = PageUtil.initPage(pageVo);
        Page<WebsiteInformation> pp= websiteInformationServiceImpl.getWebsiteInformationPage(pageable,websiteInformation);
       /* pp.getContent().stream().forEach(web->{
            web.setLogo(null);
        });*/
        CustomPage customPage = CustomPageUtil.getCustomPage(pp);
        Object resultData = customPage.getResultData();

        return    ResultUtil.data(pp);
    }
    /*
    * all
    * */
    /*
     * 网站信息分页
     * */
    @ResponseBody
    @GetMapping("/getWebsiteInformationAll")
    public Result getWebsiteInformationAll(){

        List<WebsiteInformation> list= websiteInformationServiceImpl.getWebsiteInformationAll();
        /*list.stream().forEach(web->{
            web.setLogo(null);
        });*/
        return    ResultUtil.data(list);
    }
    /*
     * 网站信息根据id获取
     * */
    @ResponseBody
    @GetMapping("/showImage")
    public void getWebsiteInformationById(String id, HttpServletRequest request, HttpServletResponse response){
        WebsiteInformation web=    websiteInformationServiceImpl.getWebsiteInformationById(id);
        if(web!=null){
            byte[] logo = web.getLogo();
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/png");
            OutputStream outputStream = null;
            try {
                outputStream = response.getOutputStream();
                outputStream.write(logo);
                outputStream.flush();//排出流中所有数据
            } catch (IOException e) {

                e.printStackTrace();
            }finally {
                try {
                    outputStream.close();//关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
